package com.blog.dao.frontpage;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.blog.dao.conn.BaseDao;
import com.blog.dao.conn.ParamSet;
import com.blog.dao.conn.RowDatas;
import com.blog.entity.ArticleConstraintEntity;
import com.blog.entity.UserEntity;
import com.blog.entity.UserLoginEntity;

public class UserDao extends BaseDao {

	private RowDatas<UserEntity> userInfo = new RowDatas<UserEntity>() {

		@Override
		public UserEntity getRowData(ResultSet rs) throws SQLException {
			UserEntity ue = new UserEntity();
			ue.setUser_password(rs.getString("user_password"));
			return ue;
		}
	};
	private RowDatas<UserEntity> passwordInfo = new RowDatas<UserEntity>() {

		@Override
		public UserEntity getRowData(ResultSet rs) throws SQLException {
			UserEntity ue = new UserEntity();
			ue.setUser_phone(rs.getString("user_phone"));
			ue.setUser_password(rs.getString("user_password"));
			ue.setUser_name(rs.getString("user_name"));
			ue.setUser_id(rs.getInt("user_id"));
			return ue;
		}
	};
	
	/**
	 * 查询用户的用户名是否存在
	 * 
	 */
	public UserEntity queryUnameForLogin(String phone) {
		String sql = "SELECT user_password from tb_user where user_phone = ?";
		return query(sql, new ParamSet(phone),userInfo);
	}

	/**
	 * 注册时向数据库插入一条数据
	 */
	public int insertUserForReg(UserEntity user) {
		int row = 0;
		String sql = "insert into tb_user(user_name,user_phone,user_password,user_ragtime)\r\n" + "values(?,?,?,?);";
		row = updateExecuted(sql, new ParamSet(user.getUser_name(), user.getUser_phone(), user.getUser_password(),
				user.getUser_ragtime()));
		return row;

	}


	public UserEntity queryPasswordForLogin1(String phone) {
		String sql = "SELECT user_phone,user_password,user_name,user_id from tb_user where user_phone = ?";
		return query(sql, new ParamSet(phone), passwordInfo);
	}
	public UserLoginEntity  queryPasswordForLogin(String phone) {
		String sql = "SELECT user_password,user_id,user_name,user_image,user_password,user_phone from tb_user where user_phone = ?";
		return query(sql, new ParamSet(phone), UserLoginEntity .class);

	}

}
